package cn.ibizlab.codegen;

import cn.ibizlab.codegen.groovy.GroovyCodeGenEngine;
import net.ibizsys.model.IPSModelObject;
import net.ibizsys.model.IPSSystem;
import net.ibizsys.model.PSModelServiceImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ObjectUtils;

import java.io.File;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class EmbedGenerator {

    private static final Log log = LogFactory.getLog(EmbedGenerator.class);

    private GeneratorParam generatorParam = new GeneratorParam();

    public GeneratorParam getGeneratorParam() {
        return generatorParam;
    }

    public EmbedGenerator(Map args) {
        if(!ObjectUtils.isEmpty(args)) {
            generatorParam.setModelPath((String) args.getOrDefault("inputSpec", ""));
            generatorParam.setOutput((String) args.getOrDefault("outputDir", ""));
        }
    }


    public List<File> generate() {

        PSModelServiceImpl psModelServiceImpl = new PSModelServiceImpl();
        psModelServiceImpl.setPSModelFolderPath(generatorParam.getModelPath());

        IPSSystem iPSSystem = psModelServiceImpl.getPSSystem();

        GroovyCodeGenEngine demoEngine = new GroovyCodeGenEngine();
        try {
            demoEngine.init("/template", true, null);
            Map<String, IPSModelObject> psModelObjectMap = new LinkedHashMap<String, IPSModelObject>();
            demoEngine.output(iPSSystem, generatorParam.getOutput(), null, psModelObjectMap, false);


            for(Map.Entry<String, IPSModelObject> entry : psModelObjectMap.entrySet()) {
                log.info(String.format("输出文件[%1$s]，模型[%2$s]", entry.getKey(), entry.getValue().getId()));
            }

        } catch (Exception e) {
            log.error(e);
        }
        return Collections.emptyList();
    }
}
